package util.thread.creatthread;


import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * Callable 创建线程
 *
 * @author: Lidong
 * @time: 2020/8/6 20:58
 **/
public class CreateThreadByCallableTest {

    private static final Logger logger = LoggerFactory.getLogger(CreateThreadByCallableTest.class);

    class MyCallable implements Callable<Object> {
        @Override
        public Object call() {
            logger.info("Hello");
            Map<String, String> map = new HashMap<>();
            map.put("key1", "test");
            return map;
        }
    }

    @Test
    public void test1() {
        MyCallable callable = new MyCallable();
        FutureTask<Object> futureTask = new FutureTask<>(callable);
        try {
            futureTask.run();
            Object object = futureTask.get();
            logger.info(object.toString());
        } catch (Exception e) {
            logger.error("Error occur:{}", e.getMessage());
        }
    }
}
